﻿using Simulation.Framework;
using UnityEngine.Events;
using UnityEngine.UI;
using UnityEngine;
using System.Collections.Generic;
using System.Collections;

namespace Easy.Game2d {

    public partial class UIPlayStatePanle : PanelBase {

        #region override
        public override void Init(params object[]  data) {
            InitComponent();
            InitAddEvent();
        }

        public override void Clear() {
            ClearComponent();
        }

        public override void Open() {
            gameObject.SetActive(true);
        }

        public override void Close() {
            gameObject.SetActive(false);
        }
        #endregion

        #region data
        public int hp = 100;//血量
        public int damage = 100;//收到的伤害
        public int value = 0;//魂之命源
        #endregion
        #region init event
        private void InitAddEvent()
        {
            EventUtil.AddListener(EventData.AddKouXueClick, AddKouXueClick);
            EventUtil.AddListener(EventData.AddHPClick, AddHPClick);
            EventUtil.AddListener(EventData.AddValueClick, AddValueClick);
        }
        private void CleraAddEvent()
        {
            EventUtil.RemoveListener(EventData.AddKouXueClick, AddKouXueClick);
            EventUtil.RemoveListener(EventData.AddHPClick, AddHPClick);
            EventUtil.RemoveListener(EventData.AddValueClick, AddValueClick);
        }
        #endregion
        #region logic

        // 带参数的点击事件
        private void AddHPClick(EventArgs evt)
        {
            hp += (int)evt.args[0];
            TexthpTextMeshProUGUI.text = damage + "/" + hp.ToString(); ;
        }
        // 带参数的点击事件
        private void AddKouXueClick(EventArgs evt)
        {
            int value= (int)evt.args[0];
            damage -= value;
            if (damage > hp) damage = hp;
            TexthpTextMeshProUGUI.text = damage+"/"+hp.ToString(); ;
            if (damage <= 0) FailGame();
        }

        // 带参数的点击事件
        private void AddValueClick(EventArgs evt)
        {
            value += (int)evt.args[0];
            TextvalueTextMeshProUGUI.text = value.ToString(); ;
        }
        #endregion

        #region btn

        private void OnButtonStopButtonClicked()
        {
            Time.timeScale=Time.timeScale == 0 ? 1 : 0;
             
        }

        private void OnButtonPageButtonClicked()
        {
            Time.timeScale = 0;
            SingleBehavior<PanelManager>.getInstance().Open<UIPagePanel>(PanelName._pagePanel);
        }

        private void OnButtonExitButtonClicked()
        {
#if UNITY_EDITOR
            UnityEditor.EditorApplication.isPlaying = false;
#else
    Application.Quit();
#endif

        }

        private void OnStoreButtonButtonClicked()
        {
            Time.timeScale = 0;
            SingleBehavior<PanelManager>.getInstance().Open<UIStorePanel>(PanelName._storePanel);
        }
        private void FailGame()
        {
            SingleBehavior<PanelManager>.getInstance().Open<UISetPanel>(PanelName._setPanel);
        }

        public void ShuaXinHuoQiu()
        {
            StartCoroutine(WaitHQ());
            IEnumerator WaitHQ()
            {
                HQImageImage.fillAmount = 1;
                while (HQImageImage.fillAmount>0)
                {
                    yield return new WaitForSeconds(1);
                    HQImageImage.fillAmount -= 0.1f;
                }
            }
        }
        public void ShuaXinShanDian()
        {
            StartCoroutine(WaitShanDian());
            IEnumerator WaitShanDian()
            {
                SDImageImage.fillAmount = 1;
                while (SDImageImage.fillAmount > 0)
                {
                    yield return new WaitForSeconds(1);
                    SDImageImage.fillAmount -= 0.1f;
                }
            }
        }
        #endregion
    }
}
